package day200.LC151;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        String s="a     b    c";
        String[] split = s.split(" ");
        System.out.println(split.length);
    }
}

class Solution {
    public String reverseWords(String s) {
        String strTrim = s.trim();
        String[] split = strTrim.split(" ");
        List<String> arrList = Arrays.stream(split).filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                if (s.isEmpty()) {
                    return false;
                }else {
                    return true;
                }
            }
        }).collect(Collectors.toList());
//        StringBuilder stringBuilder = new StringBuilder();
//        arrList.forEach(new Consumer<String>() {
//            @Override
//            public void accept(String s) {
//                stringBuilder.insert(0," "+s);
//            }
//        });
//        return stringBuilder.toString().trim();
        Collections.reverse(arrList);
        return String.join(" ",arrList);
    }
}
//法二
//class Solution {
//    public String reverseWords(String s) {
//        s = s.trim();
//        // 正则匹配连续的空白字符作为分隔符分割
//        List<String> arrList = Arrays.asList(s.split("\\s+"));
//        Collections.reverse(arrList);
//        return String.join(" ", arrList);
//    }
//}